<template>
  <div>
    <!--1视图-->

    <el-form :inline="true" :model="types" class="demo-form-inline">
      <el-form-item label="车辆类型">
        <el-input v-model="types.name" placeholder="车辆类型" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item><br>
      <el-form-item>
        <el-button type="primary" @click="openAdd">添加</el-button>
        <el-button type="danger" @click="outExcel">导出</el-button>
        <el-button type="danger" @click="readExcel">导入</el-button>
      </el-form-item>
    </el-form>

    <template>
      <el-table
          :data="tableData"
          border
          style="width: 400px">
        <el-table-column
            fixed
            prop="typeId"
            label="ID"
            width="150">
        </el-table-column>
        <el-table-column
            prop="name"
            label="车辆类型"
            width="120">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="openUpdate(scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="del(scope.row.typeId)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>


    <el-pagination
        @current-change="goPage"
        :page-size="3"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>


    <el-dialog title="添加车辆类型" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="车辆类型" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改车辆类型" :visible.sync="dialogFormVisible2">
      <el-form :model="form2">
        <el-form-item label="编号" :label-width="formLabelWidth">
          <el-input v-model="form2.typeId" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="车辆类型" :label-width="formLabelWidth">
          <el-input v-model="form2.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="导入车辆类型" :visible.sync="dialogTableVisible">
      <el-upload
          class="upload-demo"
          drag
          action="http://localhost:8070/types/readExcel"
          multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>

  </div>

</template>

<script>
export default {
  name: "BatteryView",
  data() {
    return {
      //2属性定义
      form2:{},
      dialogTableVisible:false,
      dialogFormVisible2:false,
      formLabelWidth:'120px',
      form:{},
      types:{},
      total:0,
      pageNum:1,
      tableData:[],
      dialogFormVisible:false,
    }
  }, methods: {
    //3自定义方法
    readExcel(){
      this.dialogTableVisible=true;
    },
    outExcel(){
      location.href="http://localhost:8070/types/outExcel";
    },
    del(typeId){
      this.axios.post("http://localhost:8070/types/del?typeId="+typeId,{}).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.msg);
          this.getList();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    update(){
      this.axios.post("http://localhost:8070/types/update",this.form2).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.msg);
          this.dialogFormVisible2=false;
          this.getList();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    openUpdate(row){
      this.dialogFormVisible2=true;
      this.form2=row;
    },
    add(){
      this.axios.post("http://localhost:8070/types/add",this.form).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.msg);
          this.dialogFormVisible=false;
          this.getList();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    openAdd(){
      this.dialogFormVisible=true;
      this.form={};
    },
    goPage(page){
      this.pageNum=page;
      this.getList();
    },
    getList(){
      this.axios.post("http://localhost:8070/types/list?pageNum="+this.pageNum,this.types).then(res=>{
        this.tableData=res.data.data.list;
        this.total=res.data.data.total;
      })
    }
  }, created() {
    //4.钩子函数
    this.getList();
  }
}
</script>

<style scoped>

</style>